﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Configuration;
using System.Data.SqlClient;

/// <summary>
/// Summary description for D_receivedstock
/// </summary>
namespace Bestlife.ERP.DataAccess
{
    public class D_receivedstock
    {
    public static string Pro_ID;
   public static decimal QuantityDB;
   public static string proname;
        public D_receivedstock()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public static void setstock(Bestlife.ERP.Entities.E_recevedstock stock,string user)
        {
            
            DateTime date = System.DateTime.Now;

            String connStr = ConfigurationManager.ConnectionStrings["BestlifeDBConnectionString"].ConnectionString;
            Bestlife.ERP.DataAccess.Connection.connect(connStr);//connection string pass to connection class

            //string proid = getproid(stock.Proname);
            //if (stock.Status == "Recevied")
            //{
            //    Boolean quantity = reduceStock(proid, stock.Qty, user);
            //}

            string sql = "UPDATE [Tbl_GRNrequest] SET [status] ='" + stock.Status + "',[Last_Modify] ='" + date + "',[User_Create]='" + user + "' WHERE [Or_ID]='" + stock.Orid + "' and [Pro_Name]='" + stock.Proname + "'";
            //string sql = "UPDATE [BestlifeDB].[dbo].[Tbl_GRNrequest] SET [status] ='" + stock.Status + "',[Last_Modify] ='" + date + "',[User_Create]='"+user+"'WHERE [Or_ID]='" + stock.Orid + "'and [Pro_Name]='" + stock.Proname + "'";
            Boolean edit=Bestlife.ERP.DataAccess.Connection.AddEditDel(sql);
          
                 
        }

        public static string getproid(string pro)
        {
           
            
            try
            {
                SqlDataReader rdR;
                DateTime date = System.DateTime.Now;
                String connStr = ConfigurationManager.ConnectionStrings["BestlifeDBConnectionString"].ConnectionString;
                Bestlife.ERP.DataAccess.Connection.connect(connStr);//connection string pass to connection class


                String Ssql = "SELECT [Pro_ID]FROM [Tbl_Product] where [Pro_Name]='" + pro + "'";

                rdR = Connection.ReadTable(Ssql);

                if (rdR.Read())
                {
                     Pro_ID = rdR["Pro_ID"].ToString();
                   
                }
                rdR.Close(); 
            }
            catch
            {
            }
           
            return Pro_ID;
         }
        
         public static Decimal ReturnStockQuantity(string pro_id,string code)
        {
              SqlDataReader rdR;

              
              try
              {
                  String connStr = ConfigurationManager.ConnectionStrings["BestlifeDBConnectionString"].ConnectionString;
                  Bestlife.ERP.DataAccess.Connection.connect(connStr);//connection string pass to connection class
                  string product = getproname(pro_id);
                  String Ssql = "SELECT Qty_in_stock from Tbl_Stock  where [Pro_ID]='" + pro_id + "' and [Code]='" + code + "' ";
                  //SqlDataReader ldr = Bestlife.ERP.DataAccess.clsCon.ReadTable(Ssql);


                  rdR = Connection.ReadTable(Ssql);


                  if (rdR.Read())
                  {
                      QuantityDB = Decimal.Parse(rdR["Qty_in_stock"].ToString());
                     
                  }
                  rdR.Close();
              }
              catch (Exception)
              {

              }

              return QuantityDB;
             
        }

        //public static Boolean reduceStock(string pro_id, decimal qty,string user)
        //{
        //    Boolean upd2 = false;
        //    //Decimal QuantityinDB = ReturnStockQuantity(pro_id);

        //    if (qty > 0)
        //    {
        //       // Decimal QuantityToDB = QuantityinDB + Decimal.Parse(qty.ToString());
        //        //if (QuantityToDB > 0)
        //        //{
        //           string product=getproname(pro_id);
        //            String connStr = ConfigurationManager.ConnectionStrings["BestlifeDBConnectionString"].ConnectionString;
        //        Bestlife.ERP.DataAccess.Connection.connect(connStr);//connection string pass to connection class
        //        String updtxt2 = "UPDATE Tbl_Stock_Master SET  Qty_in_stock ='" + qty + "',Last_Modify='" + System.DateTime.Now + "',[User_Create]='" + user + "' WHERE Pro_Name='" + product + "'";
        //            // String updtxt2 = "UPDATE Bonus  SET  T_commision='999' WHERE User_id='001'";
        //            Boolean edit1 = Bestlife.ERP.DataAccess.Connection.AddEditDel(updtxt2);
        //        //}
        //        //else
        //        //{
        //        //    //print we have only QuantityinDB now !!!
        //        //}
        //    }
        //    else
        //    {
        //        // sory we have nt any Product 
        //    }
        //    return upd2;
        //}

        public static string getproname(string pro)
        {
            SqlDataReader rdR;
            try
            {
                DateTime date = System.DateTime.Now;
                String connStr = ConfigurationManager.ConnectionStrings["BestlifeDBConnectionString"].ConnectionString;
                Bestlife.ERP.DataAccess.Connection.connect(connStr);//connection string pass to connection class


                String Ssql = "SELECT [Pro_Name] FROM [Tbl_Product] where [Pro_ID]='" + pro + "'";

                rdR = Connection.ReadTable(Ssql);

                if (rdR.Read())
                {
                     proname = rdR["Pro_Name"].ToString();
         
                }
                rdR.Close();
            }
            catch
            {
            }

            return proname;

        }

    }

}
    